package com.cy.pj.common.config;

import java.util.LinkedHashMap;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cy.pj.sys.service.ShiroUserRealm;

/**
 * 此类用于配置shiro拦截器以及shiro工厂
 * @author private
 *
 */
@Configuration
public class SpringShiroConfig {
	
	//注入Realm
	@Bean
	public Realm realm() {
		return new ShiroUserRealm();
	}
	
	/** 配置过滤规则 */
	@Bean
	public ShiroFilterChainDefinition shiroFilterChainDefinition() {
	    DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
	    LinkedHashMap<String, String> map = new LinkedHashMap<>();
	    map.put("/bower_components/**", "anon");
		map.put("/build/**", "anon");
		map.put("/dist/**", "anon");
		map.put("/plugins/**", "anon");
		map.put("/user/doLogin", "anon");
		map.put("/doLogout", "logout");
		//除了匿名访问的资源,其他的都要认证 - authc
		map.put("/**", "user");//更改为验证是否登录,有记住我的直接登录,没有的继续拦截
	    chainDefinition.addPathDefinitions(map);
	    return chainDefinition;
	}
	
	/** 配置缓存对象用于缓存用户权限信息 因为Spring内置也有cacheManager所以我们给方法加了名字 */
	@Bean
	protected CacheManager shiroCacheManager() {
	    return new MemoryConstrainedCacheManager();
	}
	
}
